//
//  XHVideoRateView.m
//  ShowYork
//
//  Created by hxh on 2020/9/4.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "XHVideoRateView.h"

typedef void(^doneBlock)(NSInteger index);
@interface XHVideoRateView ()
@property (nonatomic,strong)UIView *containView;
@property (nonatomic,copy)NSArray *rateArr;
@property (nonatomic,copy)doneBlock done;

@end

@implementation XHVideoRateView

- (instancetype)initWithCompleteBlock:(void (^)(NSInteger))completeBlock{
    if (self = [super init]) {
        [self setUI];
        if (completeBlock) {
            self.done = ^(NSInteger index) {
                completeBlock(index);
            };
        }
    }
    return self;
}

- (void)setUI{
    
    
    
    self.rateArr = @[@"1.5x",@"1.25x",@"1.0x",@"0.5x"];
    
    
    CGFloat maxW = kScreenW;
    CGFloat maxH = kScreenH;
    
    if ([UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationPortrait ||
    [UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationPortraitUpsideDown) {
        //当前竖屏
        maxW = kScreenH;
        maxH = kScreenW;
    }
    
    self.frame = CGRectMake(0, 0, maxW, maxH);

    self.containView = [[UIView alloc]init];
    self.containView.backgroundColor = RGBA(0, 0, 0, 0.7);
    [self addSubview:self.containView];
    
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.width.mas_equalTo(maxW/3);
        make.right.equalTo(self).offset(maxW/3);
    }];
    
    CGFloat margin = 100;
    CGFloat H = (maxH - 100 * 2)/self.rateArr.count;
    for (int i = 0; i < self.rateArr.count; i++) {
        UIButton *btn = [[UIButton alloc]init];
        [self.containView addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.containView).offset(margin + H * i);
            make.left.right.equalTo(self.containView);
            make.height.mas_equalTo(H);
        }];
        [btn setTitle:self.rateArr[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn.titleLabel.font = font(14);
        btn.tag = i;
        [btn addTarget:self action:@selector(changeRateMethod:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeCover)];
    [self addGestureRecognizer:tap];
}

- (void)changeRateMethod:(UIButton *)btn{
    if (self.done) {
        self.done(btn.tag);
    }
    [self dismiss];
}
- (void)removeCover{
    if (self.done) {
        self.done(-1);
    }
    [self dismiss];
}

- (void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    
    [self layoutIfNeeded];
    
    [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
           make.right.equalTo(self);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
}

- (void)dismiss{
    
    [self layoutIfNeeded];
    
    CGFloat maxW = kScreenW;

    if ([UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationPortrait ||
    [UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationPortraitUpsideDown) {
        //当前竖屏
        maxW = kScreenH;
    }
    [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(maxW/3);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
    
}

@end
